﻿Imports System.ComponentModel
Imports System.Xml

Namespace Objects

    Public Class DocumentBlok

        Private _document As PrintFairShopDocument
        Private _rijen As List(Of DocumentRij)
        Private _naam As String
        Private _isHerhaald As Boolean

#Region "Public property's"

        <Category("Behavior")> _
        Public Property Naam() As String
            Get
                Return _naam
            End Get
            Set(ByVal value As String)
                _naam = value
            End Set
        End Property

        <Browsable(False)> _
       Public ReadOnly Property Rijen() As List(Of DocumentRij)
            Get
                Return _rijen
            End Get
        End Property

        <Browsable(False)> _
        Public ReadOnly Property TitelRij() As DocumentRij
            Get
                If (_rijen.Count = 2) Then Return _rijen(0)
                Return Nothing
            End Get
        End Property

        <Browsable(False)> _
        Public ReadOnly Property SjabloonRij() As DocumentRij
            Get
                If (_rijen.Count = 2) Then Return _rijen(1)
                Return _rijen(0)
            End Get
        End Property

        <Category("Behavior")> _
        Public Property IsHerhaald() As Boolean
            Get
                Return _isHerhaald
            End Get
            Set(ByVal value As Boolean)
                _isHerhaald = value
            End Set
        End Property

#End Region

#Region "Constructor/initialisatie"

        Public Sub New(ByVal document As PrintFairShopDocument, ByVal blokNode As XmlNode)
            _document = document
            _naam = blokNode.Attributes("naam").Value
            _rijen = New List(Of DocumentRij)

            Select Case blokNode.Attributes("herhaald").Value
                Case "false"
                    _isHerhaald = False
                    Dim rijNodes As XmlNodeList = blokNode.SelectNodes("rijen/rij")
                    For Each rijNode As XmlNode In rijNodes
                        Dim documentRij As New DocumentRij(document, rijNode)
                        _rijen.Add(documentRij)
                    Next
                Case "true"
                    _isHerhaald = True
                    Dim titelRijNode As XmlNode = blokNode.SelectSingleNode("rijen/titelrij")
                    If (titelRijNode IsNot Nothing) Then
                        Dim titelRij As New DocumentRij(document, titelRijNode)
                        _rijen.Add(titelRij)
                    End If

                    Dim sjabloonRijNode As XmlNode = blokNode.SelectSingleNode("rijen/sjabloonrij")
                    Dim sjabloonRij As New DocumentRij(document, sjabloonRijNode)
                    _rijen.Add(sjabloonRij)
            End Select
        End Sub

#End Region

#Region "Public methods"

        Public Sub SetCelTekst(ByVal id As String, ByVal tekst As String)
            For Each rij As DocumentRij In _rijen
                For Each cel As DocumentCel In rij.Cellen
                    If (cel.ID = id) Then
                        cel.Tekst = tekst
                        Return
                    End If
                Next
            Next
            Throw New Exception(String.Format("Cel met id {0} niet gevonden", id))
        End Sub

        ''' <summary>
        ''' Zet de waarde van currency in de cel en toont deze in het rood als ze negatief is
        ''' </summary>
        ''' <param name="id"></param>
        ''' <param name="currency"></param>
        Public Sub SetCelCurrency(ByVal id As String, ByVal currency As Decimal)
            For Each rij As DocumentRij In _rijen
                For Each cel As DocumentCel In rij.Cellen
                    If (cel.ID = id) Then
                        If (currency = 0D) Then
                            cel.Tekst = ""
                        Else
                            cel.Tekst = currency.ToString("N2")
                        End If
                        If (currency < 0) Then
                            cel.ForegroundColor = Brushes.Red
                        Else
                            cel.ForegroundColor = Brushes.Black
                        End If
                        Return
                    End If
                Next
            Next
            Throw New Exception(String.Format("Cel met id {0} niet gevonden", id))
        End Sub

#End Region

    End Class

End Namespace
